﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataCoreTechnologies.SqlServer
{
   /// <summary>
   /// Contains the information about a primary key belonging to a table.
   /// </summary>
   public sealed class PrimaryKey
   {
      #region Public
      /// <summary>
      /// A number that uniquely identifies this class instance.
      /// </summary>
      public Guid InstanceId { get; internal set; }
      /// <summary>
      /// The table this primary key belongs to.
      /// </summary>
      public Table Table { get; private set; }
      /// <summary>
      /// The index this primary key uses.
      /// </summary>
      public Index Index { get; internal set; }
      /// <summary>
      /// A name used to identify this primary key in the database.
      /// </summary>
      public string Name { get; internal set; }
      /// <summary>
      /// The date this primary key was created.
      /// </summary>
      public DateTime CreateDate { get; internal set; }
      /// <summary>
      /// The last time this primary key was modified.
      /// </summary>
      public DateTime ModifyDate { get; internal set; }
      /// <summary>
      /// Indicates whether this primary key was created by the the system or a user.
      /// </summary>
      public Boolean IsMicrosoftShipped { get; internal set; }
      /// <summary>
      /// A collection of name / value pairs describing this primary key.
      /// </summary>
      public IEnumerable<ExtendedProperty> ExtendedProperties
      {
         get
         {
             for (int index = 0; index < _extendedProperties.Count; index++)
             {
                 yield return _extendedProperties[index];
             }
         }
      }
      #endregion
      #region Internal
      //Constructor
      /// <summary>
      /// Creates a instance of a primary key belonging to the table specified by the parameter.
      /// </summary>
      /// <param name="container">The table this primary key belongs to.</param>
      internal PrimaryKey(Table container)
      {
         Table = container;
         InstanceId = System.Guid.NewGuid();
      }
      //Properties
      /// <summary>
      /// A number used to uniquely identify this object in the database.
      /// </summary>
      internal int Id { get; set; }
      //Methods
      /// <summary>
      /// Associates a extended property with this primary key.
      /// </summary>
      /// <param name="extendedProperty"></param>
      internal void AddExtendedProperty(ExtendedProperty extendedProperty) { _extendedProperties.Add(extendedProperty); }
      #endregion
      #region Private
      private List<ExtendedProperty> _extendedProperties = new List<ExtendedProperty>();
      #endregion
   }
}
